/*****************************************************************************
 *
 * Weber State University
 * CS4280 Spring 2010
 * Final Group Assignment
 *
 *
 *   TTTTTT                               CCCCC
 *     TT   EEEEE   A    M   M          CC      C
 *     TT   E      A A   M   M         CC   
 *     TT   E     A   A  MM MM  -----  CC
 *     TT   EEE   AAAAA  M M M  -----  CC
 *     TT   E     A   A  M   M          CC      C
 *     TT   EEEEE A   A  M   M            CCCCC
 *
 *   Jared Pack ---- Jon Mann ---- Joshua Clayton
 *
 *
 * 
 *
 ****************************************************************************/

 /* FILENAME rocket.cpp
  *
  * Change control Section
  *
  * Team Member: Jon Mann
  * Version: 0.1
  * Date: 04/6/2010
  *     Changed the code in OnDraw() to disable the fog
  *		Added lighting code to OnDraw() and the constructor to 
  *		attach a light to the rocket (and explosion)--getting some strange results...
  *     
  * Version: 0.2
  * Date: 04/8/2010
  *     Changed the code in OnPrepare() to disable the light after the rocket explosion dies
  *		Lighting weirdness persists, but is much less noticeable
  *
  * Version: 0.3
  * Date: 04/9/2010
  *     Minor change to velocity to help with lighting problem
  *
  */

#include "rocket.h"

CRocket::CRocket()
{
	GLfloat lightIntensity[] = {0.6f, 0.5f, 0.3f, 1.0f}; // color and brightness
	glLightfv(GL_LIGHT0, GL_DIFFUSE, lightIntensity);

	velocity = CVector(0.0, 0.0, 300.0f);
	acceleration = CVector(0.0, 0.0, 0.0);
	distanceTravel = 0.0;
	size = 1.0f;
	isExplosion = false;

	explosion = NULL;
	explosionTex = new CTexture;

	Load();
}

CRocket::~CRocket()
{
	if (explosion != NULL)
	{
		explosion->KillSystem();
		delete [] explosion;
		explosion = NULL;
	}

	if (explosionTex != NULL)
	{
		delete explosionTex;
		explosionTex = NULL;
	}
}

void CRocket::SetupExplosionTexture()
{
	glGenTextures(1, &explosionTex->texID);
	glBindTexture(GL_TEXTURE_2D, explosionTex->texID);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, 4, explosionTex->width, explosionTex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, explosionTex->data);
	gluBuild2DMipmaps(GL_TEXTURE_2D, 4, explosionTex->width, explosionTex->height, GL_RGBA, GL_UNSIGNED_BYTE, explosionTex->data);
}

void CRocket::OnAnimate(scalar_t deltaTime)
{
	float cosYaw = (float)cos(DEG2RAD(direction));
	float sinYaw = (float)sin(DEG2RAD(direction)); 
	float sinPitch = (float)sin(DEG2RAD(pitch));

	float speed = velocity.z * deltaTime;

	position.x += float(cosYaw)*speed;
	position.y += float(sinPitch)*speed;
	position.z += float(sinYaw)*speed;

	distanceTravel += position.Length();

	if (isExplosion)
		explosion->Update(deltaTime);

	if (!isExplosion)
	{
		if (distanceTravel >= 1000000.0f)
		{
			isExplosion = true;
			velocity = CVector(0.0, 0.0, 0.0);
			explosion = new CExplosion(10, position, 0.1f, explosionTex->texID);
		}
	}
}

void CRocket::OnCollision(CObject *collisionObject)
{
	if (!isExplosion)
	{
		if (typeid(*collisionObject) == typeid(CTerrain))
		{
			if (((CTerrain*)collisionObject)->GetHeight(position.x, position.z) + size >= position.y)
			{
				isExplosion = true;
				velocity = CVector(0.0, 0.0, 0.0);
				explosion = new CExplosion(500, position, 8.0, explosionTex->texID);
// Phase 19 - Uncomment the following
#ifdef ENABLE_SOUND 
				PlaySound();
#endif
			}
		
			// do explosion
		}
		if (typeid(*collisionObject) == typeid(COgroEnemy))
		{
			isExplosion = true;
			velocity = CVector(0.0, 0.0, 0.0);
			explosion = new CExplosion(500, position, 8.0, explosionTex->texID);
// Phase 19 - Uncomment the following
#ifdef ENABLE_SOUND 
			PlaySound();
#endif
		}
		if (typeid(*collisionObject) == typeid(CSodEnemy))
		{
			isExplosion = true;
			velocity = CVector(0.0, 0.0, 0.0);
			explosion = new CExplosion(500, position, 8.0, explosionTex->texID);
// Phase 19 - Uncomment the following		
#ifdef ENABLE_SOUND 
			PlaySound();
#endif
		}
	}
}

void CRocket::OnDraw(CCamera *camera)
{
	// attacha light to the rocket 
	glEnable(GL_LIGHT0);
	// set the light source properties
	GLfloat light_position[] = {position.x, position.y, position.z-1.0f, 1.0f};
	glLightfv(GL_LIGHT0, GL_POSITION, light_position); // location of light number 0, out of 8 possible lights

	// if the rocket has not yet exploded, draw the rocket model
	if (!isExplosion)
	{
		glEnable(GL_TEXTURE_2D);
		glColor3f(1.0, 1.0, 1.0);
		glTranslatef(position.x, position.y, position.z);
		glRotatef(-direction, 0.0, 0.1, 0.0);
		glScalef(0.025f, 0.025f, 0.025f);
		RenderFrame(0);
		glDisable(GL_TEXTURE_2D);
	}
	// draw explosion
	else
	{
		// Phase 16 - take out fog commands until fog is defined
		// Phase 20 - Uncomment fog statements
		//glDisable(GL_FOG);
		explosion->Render();
		//glEnable(GL_FOG);
		// Phase 16 - End
	}
}


void CRocket::Load()
{
	CMD2Model::Load("models\\rocketair.md2", "models\\rocket.pcx");
	explosionTex->LoadTexture("explosion.bmp");
	SetupExplosionTexture();
}

void CRocket::Unload()
{
}

void CRocket::OnPrepare()
{
	// perform collision detection from this entity with all other objects in world
	if (!isExplosion)
		ProcessCollisions(FindRoot());

	if (isExplosion)
	{
		// Phase 16 - Take out for now
		// Phase 19 - Uncomment the following
		if (explosion->IsDead())
		{
			explosion->KillSystem();
			delete explosion;
			explosion = NULL;
			isExplosion = false;

			isDead = true;

			// kill the light when the explosion is done
			glDisable(GL_LIGHT0);
		}
	}
}
